package com.speechb.common.file;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 属性文件获取工具类
 * @author : llj
 * @version :2.1.0
 * @date : 2019/7/16 19:55
 */
public class PropertyUtils {
    /**
     * 获取属性文件对象
     * @param propertiesFilePath 资源目录下路径
     * @return
     * @throws IOException
     */
    public static Properties getProperties(String propertiesFilePath) throws IOException {
        if(StringUtils.isEmpty(propertiesFilePath)) {
            return null;
        }
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFilePath);
        OrderedProperties prop = new OrderedProperties();
        prop.load(is);
        return prop;
    }

    /**
     * 将属性文件的key和value转换为map集合
     * @param propertiesFilePath 资源目录下路径
     * @return
     * @throws IOException
     */
    public static Map<String,String> propertiesToMap(String propertiesFilePath) throws IOException {
        Map<String, String> map = new LinkedHashMap<>();
        Properties prop = getProperties(propertiesFilePath);
        if (Objects.nonNull(prop)) {
            Enumeration<?> propertyNames = prop.propertyNames();
            while (propertyNames.hasMoreElements()) {
                String key = (String) propertyNames.nextElement();
                String value = (String) prop.get(key);
                map.put(key,value);
            }
        }
        return map;
    }
}
